package ocean.chinacraft.common.world.item.spell;

import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.world.World;
import ocean.chinacraft.common.config.GroupDefine;

import javax.annotation.Nonnull;

/**
 * 护符
 *
 * @author zhang
 * @since 2021/12/15
 */
public class ProtectSpell extends Item {

    private final int time = 2 * 20 * 60;

    public ProtectSpell() {
        super(new Properties().tab(GroupDefine.CHINACRAFT_SPELL));
    }

    @Override
    public boolean onLeftClickEntity(ItemStack stack, PlayerEntity player, Entity entity) {
        if (entity.isAlive()) {
            LivingEntity livingEntity = (LivingEntity) entity;
            livingEntity.addEffect(new EffectInstance(Effects.FIRE_RESISTANCE, time, 0, false, false));
            livingEntity.addEffect(new EffectInstance(Effects.DAMAGE_RESISTANCE, time, 0, false, false));
        }
        if (!player.isCreative()) {
            stack.shrink(1);
        }
        return true;
    }

    @Override
    public ActionResult<ItemStack> use(@Nonnull World world, PlayerEntity player, @Nonnull Hand hand) {
        player.addEffect(new EffectInstance(Effects.FIRE_RESISTANCE, time, 0, false, false));
        player.addEffect(new EffectInstance(Effects.DAMAGE_RESISTANCE, time, 0, false, false));
        ItemStack itemInHand = player.getItemInHand(hand);
        if (!itemInHand.isEmpty() && !player.isCreative()) {
            itemInHand.shrink(1);
        }
        return ActionResult.success(itemInHand);
    }
}
